package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SupplierEnterprise;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.dto.EnterpriseSupplierDTO;
import com.ruoyi.system.mapper.SupplierEnterpriseMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EnterpriseSupplierMapper;
import com.ruoyi.system.domain.EnterpriseSupplier;
import com.ruoyi.system.service.IEnterpriseSupplierService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 企业对应合作商关系Service业务层处理
 *
 * @author 协同
 * @date 2022-04-18
 */
@Service
public class EnterpriseSupplierServiceImpl implements IEnterpriseSupplierService {
    @Resource
    private EnterpriseSupplierMapper enterpriseSupplierMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SupplierEnterpriseMapper supplierEnterpriseMapper;

    /**
     * 查询企业对应合作商关系
     *
     * @param id 企业对应合作商关系主键
     * @return 企业对应合作商关系
     */
    @Override
    public EnterpriseSupplier selectEnterpriseSupplierById(Long id) {
        return enterpriseSupplierMapper.selectEnterpriseSupplierById(id);
    }

    /**
     * 查询企业对应合作商关系列表
     *
     * @param enterpriseSupplierDTO 企业对应合作商关系
     * @return 企业对应合作商关系
     */
    @Override
    public List<EnterpriseSupplier> selectEnterpriseSupplierList(EnterpriseSupplierDTO enterpriseSupplierDTO) {
        enterpriseSupplierDTO.setEnterpriseId(SecurityUtils.getLoginUser().getUserId());
        return enterpriseSupplierMapper.selectEnterpriseSupplierList(enterpriseSupplierDTO);
    }

    /**
     * 修改企业对应合作商关系
     *
     * @param enterpriseSupplier 企业对应合作商关系
     * @return 结果
     */
    @Override
    public int updateEnterpriseSupplier(EnterpriseSupplier enterpriseSupplier) {
        enterpriseSupplier.setUpdateTime(DateUtils.getNowDate());
        return enterpriseSupplierMapper.updateEnterpriseSupplier(enterpriseSupplier);
    }

    /**
     * 新增企业对应合作商关系
     *
     * @param enterpriseSupplierDTO 企业对应合作商关系
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertEnterpriseSupplier(EnterpriseSupplierDTO enterpriseSupplierDTO) {
        String msg;
        if (sysUserMapper.checkUserNameUnique(enterpriseSupplierDTO.getSupplierName()) > 0) {
            msg = "新增供应商'" + enterpriseSupplierDTO.getSupplierName() + "'失败，登录账号已存在";
        } else {
            SysUser user = new SysUser();
            user.setUserName(enterpriseSupplierDTO.getSupplierName());
            user.setNickName(enterpriseSupplierDTO.getSupplierName());
            user.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            user.setPassword(SecurityUtils.encryptPassword("123456"));
            user.setDeptId(102L);
            // 新增供应商信息
            sysUserMapper.insertUser(user);
            // 新增供应商与角色关联
            SysUserRole ur = new SysUserRole();
            ur.setUserId(user.getUserId());
            ur.setRoleId(101L);
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            list.add(ur);
            sysUserRoleMapper.batchUserRole(list);

            // 企业对应合作商关系
            EnterpriseSupplier supplier = new EnterpriseSupplier();
            supplier.setEnterpriseId(SecurityUtils.getLoginUser().getUserId());
            supplier.setSupplierId(user.getUserId());
            supplier.setLevel(enterpriseSupplierDTO.getLevel());
            supplier.setCooperationStatus("1");
            supplier.setMark(enterpriseSupplierDTO.getMark());
            supplier.setCreateTime(DateUtils.getNowDate());
            enterpriseSupplierMapper.insertEnterpriseSupplier(supplier);

            // 合作商对应企业关系
            SupplierEnterprise supplierEnterprise = new SupplierEnterprise();
            supplierEnterprise.setEnterpriseId(SecurityUtils.getLoginUser().getUserId());
            supplierEnterprise.setSupplierId(user.getUserId());
            supplierEnterpriseMapper.insertSupplierEnterprise(supplierEnterprise);
            msg = "新增合作商成功";
        }


        return msg;
    }

    /**
     * 停止企业对应合作商关系
     *
     * @param ids 企业对应合作商关系
     * @return 结果
     */
    @Override
    public int updateByIds(Long[] ids) {
        return enterpriseSupplierMapper.updateByIds(ids);
    }

    /**
     * 批量删除企业对应合作商关系
     *
     * @param ids 需要删除的企业对应合作商关系主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseSupplierByIds(Long[] ids) {
        return enterpriseSupplierMapper.deleteEnterpriseSupplierByIds(ids);
    }

    /**
     * 删除企业对应合作商关系信息
     *
     * @param id 企业对应合作商关系主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseSupplierById(Long id) {
        return enterpriseSupplierMapper.deleteEnterpriseSupplierById(id);
    }
}
